package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.excel.model.DeptCheckRecord;
import com.cn.jdia.tas.excel.model.PersonCheckRecord;
import com.cn.jdia.tas.model.T_attrecord;
import com.cn.jdia.tas.model.T_employee;
import com.cn.jdia.tas.service.IAttrecordService;
import com.cn.jdia.tas.service.IEmployeeService;
import com.cn.jdia.tas.test.EasypoiTestModel;
import com.cn.jdia.tas.utils.EasyPoiUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class ExcelController {

	@Resource
	private IAttrecordService attService = null;

	@Resource
	private IEmployeeService employeeService = null;

	@RequestMapping("/personexcel")
	public String getPersonMonthExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String id = map.get("pid");
		String start = map.get("stvalue");
		String end = map.get("edvalue");

		DateTime dt = new DateTime();
		// System.out.println(start + " " + end);

		String timestap = dt.toString("yyyyMMddhhmmss");

		List<T_attrecord> attrecordList = attService.getDetailAttrecords(id, start, end);
		T_attrecord tmp = null;

		List<PersonCheckRecord> checklist = new ArrayList<>();

		// 第一天日期
		String tmpdate = "";
		PersonCheckRecord rcd = new PersonCheckRecord();

		for (int i = 0; i < attrecordList.size(); i++) {
			tmp = attrecordList.get(i);

			// 新
			if (!tmp.getCheckdate().equals(tmpdate)) {
				checklist.add(rcd);

				rcd = new PersonCheckRecord();
				rcd.setDate(tmp.getCheckdate());
				rcd.setStart(tmp.getChecktime());

				tmpdate = tmp.getCheckdate();
			} else {// 老
				rcd.setEnd(tmp.getChecktime());
			}

			// System.out.println(tmp.getCheckdate()+" "+tmp.getChecktime());
		}
		checklist.add(rcd);
		checklist.remove(0);
		// String s = gson.toJson(checklist);
		// System.out.println(s);

		String path = request.getSession().getServletContext().getRealPath("/");
		EasyPoiUtil.exportExcelToMobile(checklist, "打卡记录", id + "出勤记录", PersonCheckRecord.class, id + timestap + ".xls",
				response, path + "tempfile\\");

		out.write("tempfile/" + id + timestap + ".xls");
		out.flush();
		out.close();

		return null;
	}

	@RequestMapping("/deptexcel")
	public String getDeptMonthExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String pid = map.get("pid");
		String start = map.get("stvalue");
		String end = map.get("edvalue");

		T_employee employee = employeeService.getEmployeeInfoById(pid);
		// System.out.println(employee.getDeptid() + " " +
		// employee.getDepatment().getParentdeptid() + ""
		// + employee.getDepatment().getDeptname());
		String deptid = null;
		List<T_attrecord> deptRecords = null;
		if (employee.getDepatment().getParentdeptid().equals("1")) {
			deptid = employee.getDeptid();
			deptRecords = attService.getChildDepetMutilRecords(deptid, start, end);
		} else {
			deptid = employee.getDepatment().getParentdeptid();
			deptRecords = attService.getParentDepetMutilRecords(deptid, start, end);
		}

		System.out.println(deptRecords.size());

		List<DeptCheckRecord> checklist = new ArrayList<>();
		DeptCheckRecord rcd = new DeptCheckRecord();

		T_attrecord tmp = null;

		String tmpdate = "";
		String tmpid = "";

		for (int i = 0; i < deptRecords.size(); i++) {
			tmp = deptRecords.get(i);
			// System.out
			// .println(tmp.getEmployee().getEmployeename() + " " +
			// tmp.getCheckdate() + " " + tmp.getChecktime());

			if ((!tmp.getCheckdate().equals(tmpdate)) || (!tmp.getEmployeeid().equals(tmpid))) {
				checklist.add(rcd);

				rcd = new DeptCheckRecord();
				rcd.setName(tmp.getEmployee().getEmployeename());
				rcd.setDate(tmp.getCheckdate());
				rcd.setStart(tmp.getChecktime());
				
				rcd.setAttmachinename(tmp.getAttmachinename());

				tmpdate = tmp.getCheckdate();
				tmpid = tmp.getEmployeeid();
			} else {
				rcd.setEnd(tmp.getChecktime());
			}
		}
		checklist.add(rcd);
		checklist.remove(0);

		String s = gson.toJson(checklist);
		System.out.println(s);

		DateTime dt = new DateTime();
		String timestap = dt.toString("yyyyMMddhhmmss");

		String path = request.getSession().getServletContext().getRealPath("/");
		EasyPoiUtil.exportExcelToMobile(checklist, "打卡记录", "部门出勤记录", DeptCheckRecord.class, pid + timestap + ".xls",
				response, path + "tempfile\\");

		out.write("tempfile/" + pid + timestap + ".xls");
		out.flush();
		out.close();

		return null;
	}
	
}
